package com.example.novelweb.config;


import com.example.novelweb.model.entity.Wordrule;
import com.example.novelweb.service.WordruleService;
import com.example.novelweb.utils.WordCheck.WordCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Component
public class WordCheckInitTask implements ApplicationRunner {

  @Resource
  private WordruleService wordruleService;

  @Override
  public void run(ApplicationArguments args) throws Exception {
    List<Wordrule> list = wordruleService.query().select("word").eq("status",0).list();
    Set<String> words = list.stream()
        .map(Wordrule::getWord)
        .collect(Collectors.toSet());
    WordCheckUtil.addWords(words);

    log.info("初始化违禁词DFATree完毕,共"+words.size()+"个违禁词");
  }

}
